package com.example.crawler.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.example.crawler.mapper.HuShenMapper;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/bj/v1/")
public class DownloadPDFController {

    @Resource
    private HuShenMapper huShenMapper;

//    @GetMapping(value = "/getBJ")
//    @ApiOperation("爬取北交所拟上市数据")
    public void getBJ() {
        for (int i = 0; i <= 168; i++) {
            execute1(i);
        }
        System.out.println("执行结束！！！！！！！！！");

    }

    private void execute1(int i) {
        // 网址
        String url = "http://www.bse.cn/disclosureInfoController/zoneInfoResult.do?callback=jQuery331_1668567064387";
        HttpClient httpClient = new HttpClient();

        //创建HttpGet请求
        PostMethod postMethod = null;
        postMethod = new PostMethod(url);
        postMethod.setRequestHeader("User-Agent", "PostmanRuntime/7.29.2");
        NameValuePair[] data = {
                new NameValuePair("page", String.valueOf(i)),
                new NameValuePair("disclosureType", "9530"),
                new NameValuePair("startTime", "2021-11-16"),
                new NameValuePair("endTime", "2022-11-16")
        };
        postMethod.setRequestBody(data);

        CloseableHttpResponse response = null;
        execute2(httpClient, postMethod, response);
    }

    private void execute2(HttpClient httpClient, PostMethod postMethod, CloseableHttpResponse response) {
        try {
            //使用HttpClient发起请求
            httpClient.executeMethod(postMethod); // 执行POST方法
            String result = postMethod.getResponseBodyAsString();
            //打印数据
            System.out.println(result.substring(24, result.length() - 1));
            if (!StringUtils.isEmpty(result.substring(24, result.length() - 1))) {
                List<Map> maps = JSONArray.parseArray(result.substring(24, result.length() - 1), Map.class);
                List<Map> mapList = JSONArray.parseArray(JSON.parseObject(maps.get(0).get("listInfo").toString(), Map.class).get("content").toString(), Map.class);

                for (Map map : mapList) {
                    huShenMapper.insertBJ(map);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //释放连接
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
//                httpClient.close();
            }
        }
    }


}
